# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: DCPDialog.py
  @time: 2022/4/13 16:32
  @desc:
"""

from PyQt5.QtCore import QSize, Qt, QRect, pyqtSignal
from PyQt5.QtGui import QIcon, QBrush, QColor, QPen, QFont, QPainter
from PyQt5.QtWidgets import QWidget, QDialog, QVBoxLayout, QGridLayout, QComboBox, QPushButton, QLabel, QMessageBox

DCP_TITLE = ["DCP 01", "DCP 02", "DCP 03", "DCP 04", "DCP 05", "DCP 06", "DCP 07", "DCP 08", "DCP 09", "DCP 10",
             "DCP 11", "DCP 12", "RF 01", "RF 02"]

DCP_TYPES = ["None", "SMU1", "SMU2", "SMU3", "SMU4", "SMU5", "SMU6", "SMU7", "SMU8", "SMU9", "SMU10"]


class DCPDialog(QDialog):
    send_signal = pyqtSignal(object)

    def __init__(self, parent=None):
        super().__init__(parent)
        with open("./static/style_sheet/origin.qss", mode='r', encoding='utf-8') as f:
            style_sheet = f.read()
        self.setStyleSheet(style_sheet)
        self.setWindowTitle("  DCP  DEFINE")
        self.setWindowIcon(QIcon("./static/style_sheet/icons/logo.png"))
        self.resize(440, 600)
        self.vl_dcp = QVBoxLayout(self)
        self.vl_dcp.setContentsMargins(0, 0, 0, 0)
        self.wd_dcp = QWidget()
        self.gl_dcp = QGridLayout(self.wd_dcp)
        row = 0
        for dcp_name in DCP_TITLE:
            cb_dctitle = QComboBox()
            cb_dcp = QComboBox()
            cb_dcp.addItems(DCP_TYPES)
            # cb_dcp.setCurrentIndex(current_sub_die_info[row])
            self.gl_dcp.addWidget(QLabel(dcp_name), row, 0)
            self.gl_dcp.addWidget(cb_dcp, row, 1)
            row += 1
        self.pb_submit = QPushButton("提交")
        self.gl_dcp.addWidget(self.pb_submit, 15, 0, 1, 2)
        self.vl_dcp.addWidget(self.wd_dcp)
        self.pb_submit.clicked.connect(self.send_content)

    def send_content(self):
        result = self.get_pair_smu_dcp()
        final_result = list(map(self.is_none, result.values()))
        if not any(final_result):
            self.show_critical("警告", "请先设置DCP探针和SMU之间的对应关系")
        else:
            if final_result.count(True) >= 2:
                self.send_signal.emit(result)
            else:
                self.show_critical("警告", "请至少设置两个SMU")

    def is_none(self, value):
        return True if value.strip() != "None" else False

    def get_pair_smu_dcp(self):
        smu_dcp = {}
        for i in range(1, len(self.wd_dcp.children()) - 1, 2):
            if i < len(self.wd_dcp.children()) - 1:
                smu_dcp[self.wd_dcp.children()[i].text()] = self.wd_dcp.children()[i + 1].currentText()
        if all(smu_dcp.values()):
            return smu_dcp

    def show_critical(self, title, content):
        QMessageBox.critical(self, title, content)
